Skip to content

Abort in my_malloc() when an allocation fails - #417

Open
suraiya-ctrl wants to merge 1 commit into
Blosc:mainfrom
suraiya-ctrl:check-scratch-alloc
Open

suraiya-ctrl wants to merge 1 commit into
Blosc:mainfrom
suraiya-ctrl:check-scratch-alloc

Conversation

@suraiya-ctrl

@suraiya-ctrl suraiya-ctrl commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

my_malloc()'s callers in blosc_getitem(), serial_blosc(), t_blosc() and init_threads() size the scratch buffers from the blocksize in the compressed header (up to BLOSC_MAX_BLOCKSIZE, so roughly 2.1 GB of scratch) and none of them check the result. On a failed allocation tmp was NULL and tmp2/tmp3 were NULL + blocksize, so blosc_d() wrote decompressed bytes to an address picked by the header.

Per review this now aborts in my_malloc() instead of threading a check through every call site. The message moved to stderr, which is unbuffered and so still reaches the user after the abort().

Repro: a 64-byte buffer with version 2, blosclz, flags 0x10, typesize 1, nbytes = blocksize = BLOSC_MAX_BLOCKSIZE, cbytes 64, bstarts[0] = 20, split cbytes 40, then 40 bytes of 0x41, passed to blosc_getitem(buf, 0, 1, out) in an ASan build under allocator_may_return_null=1:max_allocation_size_mb=1000. Before: SEGV WRITE at 0x2aaaa956 in blosclz_decompress <- blosc_d <- blosc_getitem. Now: SIGABRT with the message. The same buffer through blosc_decompress() behaves the same way. All 1646 ctest cases pass.

@kalvdans kalvdans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think a better change is to abort() the program in my_malloc() if an allocation fails.

The scratch buffers in blosc_getitem(), serial_blosc(), t_blosc() and
init_threads() are sized from the blocksize in the compressed header and
none of them check the result, so a failed allocation left tmp NULL and
tmp2/tmp3 at NULL + blocksize, and blosc_d() then wrote decompressed
bytes to an address picked by the header. Callers cannot do anything
useful with a NULL here, so fail loudly in my_malloc() instead.

The message now goes to stderr, which is unbuffered and therefore still
reaches the user after the abort().
@suraiya-ctrl suraiya-ctrl changed the title check scratch allocations sized from the header blocksize Abort in my_malloc() when an allocation fails Nov 26, 2026
@suraiya-ctrl

Copy link
Copy Markdown
Contributor Author

Makes sense, that's simpler. Switched to abort() in my_malloc() and dropped the four call-site checks. I moved the message to stderr while I was there, since it's unbuffered and abort() isn't guaranteed to flush stdout.

I also dropped the blosc_getitem() hunk that moved the allocation below the start/nitems checks. That was fixing a small leak on those early returns, so I can send it separately if you want it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants